// index.js
const $api= require('../utils/api').API
// 获取应用实例
const app = getApp()
Page({
  data: {
    // 头部导航栏的高度
    statusBarHeight: app.globalData.statusBarHeight,
    // statusBarTop: -100,
    boundingHeight:  null,
    searchValue: '',
    allWidth: null,
    // swipeList: ["../../img/huazhong.png"],
    swipeList: [],
  /* 旅游指南的数据 */
    travelList:[
    ],
    moduleId: 0,
    baseURL:'https://j53421893f.zicp.fun',//配置的域名。
    //登录测试
    username: "admin",
    password: "admin123",
    rememberMe: 0,
    name: null
  },
  /* 跳转到搜索页 */
  SkipSearchPage(){

  },
  /* 旅游指南*/
  TravelGuide(){

  },
  seatchStore(){
      console.log("1242@");
      app.globalData.storeName = this.data.name
    wx.switchTab({
        url: '../homestay/homestay' ,
      })
  },
//跳转全部推荐
  ToAlltuijian(){
    wx.navigateTo({
      url: '../viewrecommend/viewrecommend',
    })
  },
  /* 列表子项详细信息 */
  DetaiItems(e){
    // console.log('1243413:',e.currentTarget.dataset.item.id);
    let id = e.currentTarget.dataset.item.id
    let moduleId = e.currentTarget.dataset.item.moduleId
    wx.navigateTo({
      url: '../zhongcao_detail/zhongcao_detail?id='+ encodeURIComponent(id) + "&moduleId=" + encodeURIComponent(moduleId),
    }) 
  },
   onShow() {
    let data = {
      "moduleId": 1
     }
     /* 查询轮播图片 */
     $api.selectFiles(data).then(res => {
       if(res.data.code !== 200){
        // wx.showToast({ //弹出框
        //   title: '查询文件失败！',
        //   icon: 'error',
        //   duration: 2000
        // })
       }else{
        // wx.showToast({
        //   title: '查询文件成功！',
        //   icon: 'success',
        //   duration: 2000
        // });
        //console.log(res.data.rows[0].filePath);
        /* 保存图片到轮播图数组 */
        this.data.swipeList = []
        for(let i = 0; i < res.data.rows.length;i++){
          /* 将图片的src地址拼接后push进轮播图数组。 */
          this.data.swipeList.push(this.data.baseURL + res.data.rows[i].filePath);
        }
        this.setData({ swipeList: this.data.swipeList });
       }
     })

     let data1 = {
      "moduleId": 1,
      "pageSize": 5
     }
     /* 查询列表内容 */
     $api.selectContentList(data1).then(res =>{
      if(res.data.code !== 200){
        wx.showToast({ //弹出框
          title: '查询列表失败！',
          icon: 'error',
          duration: 2000
        })
       }else{
        // wx.showToast({
        //   title: '查询列表成功！',
        //   icon: 'success',
        //   duration: 2000
        // });     
        this.data.travelList = res.data.rows;
        console.log(this.data.travelList);   
        for(let i = 0; i <this.data.travelList.length;i++){
            this.data.travelList[i].src = this.data.baseURL + this.data.travelList[i].src;
            // let data2 = {
            //     "outerId":this.data.travelList[i].id,
            //     "moduleId": 2,
            //     "innerId":1,
            //    }
            // $api.selectFiles(data2).then(res2 => {
            //     if(res2.data.code == 200){
            //        this.data.travelList.push(obj)
            //      if(res2.data.rows.length>0)
            //        this.data.travelList[i].src = this.data.baseURL+res2.data.rows[0].filePath;
            //        this.setData({travelList:this.data.travelList})
            //        console.log(this.data.travelList);
            //     }else{       
            //     }
            //   })
          }
        this.setData({travelList:this.data.travelList})
        console.log(this.data.travelList);
      }
     })
    
     let data2 = {
      "moduleId": 2,
      "innerId":1
     }
      /* 查找列表图片 */
     $api.selectFiles(data2).then(res => {
       if(res.data.code !== 200){
        wx.showToast({ //弹出框
          title: '查询图片失败！',
          icon: 'error',
          duration: 2000
        })
       }else{
        /* 保存图片到数组 */
        console.log(this.data.travelList);
        //console.log(res.data.rows[0].filePath);
        for(let i = 0; i < res.data.rows.length;i++){
          /* this.data.travelList */
          let obj = {}
        //   this.data.travelList.push(obj)
        //   this.data.travelList[i].src = this.data.baseURL+res.data.rows[i].filePath;
        }
        /* 给数组中每个元素增加一个src属性，通过for循环 */
        /* 渲染数据的作用 */
        // this.setData({ travelList: this.data.travelList });
       }
     })

    
  },
  onReady() {
    /* 通过控制函数延时时间来控制赋值时间 */
    //console.log('this',this);
    setTimeout(fn,0)
    let that = this
    function fn() {
      //console.log('this',this);
      that.setData({
        boundingHeight: getApp().globalData.boundingHeight,
        // statusBarTop: 0
      })
    }

     /* 测试登录 */
     let data3 = {
      "username": this.data.username,
      "password": this.data.password,
      "rememberMe": "0"
    }
    $api.userLogin(data3).then(res => {
      //console.log(res);
      if (res.data.code !== 200) {
        wx.showToast({ //弹出框
          title: '登录失败！',
          icon: 'error',
          duration: 2000
        });
      } else {
        wx.showToast({
          title: '自动登录成功！',
          icon: 'success',
          duration: 2000
        });
        wx.setStorageSync('token', res.data.token);
        //console.log(wx.getStorageSync('token'));
       /*  wx.getStorageSync('token'); */
      }
    })
  }
})